/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.hw.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class TimeUtils {

    /**
     * 获取当前系统时间，毫秒级
     * 
     * @return
     */
    public static long getCurrentTimesInMillSeconds() {

        return System.currentTimeMillis();
    }
    
    
    /**
     * @param format 时间格式化标准  eg YYYYMM
     * @return
     */
    public static String getCurrentTime(String format) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    /**
     * 当前天前移(-)或后移(+)天后的时间(取移动后那天的23:59:59)
     * @param time 时间
     * @param num 天数
     * @return 
     */
    public static Date getTime(Date time, int num){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
	    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + num);
	    calendar.set(Calendar.HOUR_OF_DAY, 23);
	    calendar.set(Calendar.MINUTE, 59); 
	    calendar.set(Calendar.SECOND, 59);  
	    Date date = calendar.getTime();
		return date;
	}
	
	/**
	 * 输入时间是否在指定时间段内
	 * @param date 待比较的时间
	 * @param beginTime (开始时间，如13:00:00)
	 * @param endTime (结束时间01:00:00)
	 * @param isNextDayEndTime (是否隔天)
	 * @return 
	 */
	public static boolean checkDateIsBetweenTimes(Date date, String beginTime,String endTime, boolean isNextDayEndTime) {
		
		boolean isBetween = false;
		
		try {
	    	
	    	String[] beginTimeArr =  beginTime.split(":");
	    	String[] endTimeArr =  endTime.split(":");
	    	
	    	int beginHour = Integer.parseInt(beginTimeArr[0]);
	    	int beginMinute = Integer.parseInt(beginTimeArr[1]);
	    	int beginSecond = Integer.parseInt(beginTimeArr[2]);
	    	
	    	int endHour = Integer.parseInt(endTimeArr[0]);
	    	int endMinute = Integer.parseInt(endTimeArr[1]);
	    	int endSecond = Integer.parseInt(endTimeArr[2]);
	    	
	    	Calendar c = Calendar.getInstance();
	    	c.setTime(date);
	    	int hour = c.get(Calendar.HOUR_OF_DAY);
	    	int minute = c.get(Calendar.MINUTE);
	    	int sencond = c.get(Calendar.SECOND);
	    	
	    	// 判断输入时间是否大于等于开始时间
	    	if(hour > beginHour){
	    		isBetween = true;
	    	}else if(hour == beginHour){
	    		if(minute >= beginMinute){
	    			isBetween = true;
	    		}else if (minute == beginMinute) {
					if (sencond >= beginSecond) {
						isBetween = true;
					}
				}
	    	}
	    	
	    	if(isNextDayEndTime){
	    		//是隔天结束时间的话,如果开始时间判断是中间的话,就一定是中间时间,所以只需要判断非中间时间
	    		if("0".equals(isBetween)){
	    			if(hour < endHour){
	            		isBetween = true;
	            	}else if(hour == endHour){
	            		if(minute < endMinute){
	            			isBetween = true;
	            		}else if (minute == endMinute) {
							if (sencond < endSecond) {
								isBetween = true;
							}
						}
	            	}
	    		}
	    		
	    	} else {
	    		//非隔天结束时间的话,只需要判断是中间时间的
	    		if("1".equals(isBetween)){
	    			if(hour > endHour){
	            		isBetween = false;
	            	}else if(hour == endHour){
	            		if(minute > endMinute){
	            			isBetween = false;
	            		}else if (minute == endMinute) {
							if (sencond > endSecond) {
								isBetween = false;
							}
						}
	            	}
	    		}
	    	}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			isBetween = false;
		}
		
	    return isBetween;
    }
    
    public static void main(String[] args) {
       System.out.println( getCurrentTime("yyyyMMddHHmmss"));
    }
}
